/*
Copyright (C) 2016  Marien Raat

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "boostCounter.h"

#include <stdio.h>
#include <iostream>

BoostCounter::BoostCounter()
{

}

void BoostCounter::initialize(sf::Texture *overlayTexture,
                              sf::Texture *pointerTexture,
                              sf::Font *boostCountFont)
{
    this->overlayTexture = overlayTexture;
    this->pointerTexture = pointerTexture;
    this->boostCountFont = boostCountFont;

    size = overlayTexture->getSize();
}

void BoostCounter::draw(sf::RenderTarget *target, float boostLeft)
{
    overlaySprite.setTexture(*overlayTexture);
    pointerSprite.setTexture(*pointerTexture);
    boostCount.setFont(*boostCountFont);

    sf::View currentView = target->getView();
    sf::Vector2f viewSize = currentView.getSize();

    overlaySprite.setPosition(viewSize.x - size.x - 20.0f,
                              viewSize.y - size.y - 20.0f);

    pointerSprite.setOrigin(pointerTexture->getSize().x / 2.0f,
                            pointerTexture->getSize().y);
    pointerSprite.setPosition(sf::Vector2f(size.x / 2, size.y / 2) +
                              overlaySprite.getPosition());
    pointerSprite.setRotation(270 * (boostLeft / 100.0f) - 135);

    boostCount.setCharacterSize(50);
    boostCount.setColor(sf::Color(0, 0, 0));
    char buffer[5];
    sprintf(buffer, "%d", (int) boostLeft);
    boostCount.setString(buffer);
    sf::FloatRect boundingBox = boostCount.getGlobalBounds();

    boostCount.setOrigin(boundingBox.width / 2,
                         boundingBox.height / 2);
    boostCount.setPosition((int) (overlaySprite.getPosition().x +
                                  (size.x / 2)),
                           (int) (overlaySprite.getPosition().y +
                                  (size.y * 0.75f)));

    target->draw(overlaySprite);
    target->draw(pointerSprite);
    target->draw(boostCount);
}
